package com.nf.health.app.customview;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.miansearch.adapter.DefaultSelectCallback;
import com.nf.health.app.activity.miansearch.adapter.ListThirdAdapter;
import com.nf.health.app.activity.miansearch.impl.OnChecktemLinstener;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ViewUtil;

import java.util.LinkedList;

//import android.view.ViewGroup.LayoutParams;


/**
 * 只显示文字的工具类
 * 目前在登录的时候用到
 * @author yyh
 */
public class ShowMyTextSearchPopupWindow3 extends PopupWindow implements PopupWindow.OnDismissListener,AdapterView.OnItemClickListener,View.OnClickListener{
    private ListView lv_seach_thid_view;
    private LinearLayout ll_content_search_view;
    private View mMenuView;
    private Context cnt;
    private int screenWidth = 0;
    private int screenHeight = 0;
    private View view_blank;
    private DefaultSelectCallback selectCallback;
    protected ListThirdAdapter listThirdAdapter;
    private LinkedList<String> linkedList = new LinkedList<>();
    private boolean isOpenLoadtion = false;//是不是开启定位
    /**** 是否是第一次进入,用于判断刚进入此界面时显示默认的listView中的条目 *******/
    private boolean flag = true;
    /*** 当前点击的一级条目 *****/
    private int nowPosition = 0;
    public ShowMyTextSearchPopupWindow3(Activity context) {
        cnt = context;
        initData(DensityUtil.getScreenIntWidth(cnt),DensityUtil.getScreenIntHeight(cnt));
    }
    /**
     *
     * @param context 上下文对象
     */
    public ShowMyTextSearchPopupWindow3(Activity context, int width, int height) {/*,int width,int height*/
        super(context);
        cnt = context;
        initData(width,height);
    }
    private void initData(int width, int height){
        setAnimationStyle(R.style.anim_menu_bottombar);
        LayoutInflater inflater = (LayoutInflater) cnt
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.list_search_menu, null);
        mMenuView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //popwin_layout.xml的根Layout必须为LinearLayout；如果为RelativeLayout的话， 代码出现空指针错误，导致程序崩溃
        lv_seach_thid_view = (ListView) mMenuView.findViewById(R.id.lv_seach_thid_view);
        ll_content_search_view = (LinearLayout) mMenuView.findViewById(R.id.ll_content_pop);
        view_blank =  mMenuView.findViewById(R.id.view_blank);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        //this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        //this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        screenWidth = DensityUtil.getScreenIntWidth(cnt);
        screenHeight = DensityUtil.getScreenIntHeight(cnt);
        this.setWidth(width);
        this.setHeight(height);
        this.setBackgroundDrawable(dw);
    /*    WindowManager.LayoutParams lp = (Activity)getWindow().getAttributes();
        lp.alpha = 0.7f;
        getWindow().setAttributes(lp);*/
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.ll_content_pop).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        setMyContentViewSisibile(1);
        setOnDismissListener(this);
        view_blank.setOnClickListener(this);
        lv_seach_thid_view.setOnItemClickListener(this);
        view_blank.setAlpha(0.1f);
    }
    public ShowMyTextSearchPopupWindow3 setMyContentViewSisibile(int i){
        switch (i){
            case 1://显示ListView
                break;

            case 2://显示其他
                break;
        }
        return this;
    }

    /**
     * 设置popopwindow
     */
    public ShowMyTextSearchPopupWindow3 setMyLayputParams(double wf,double hf){
        int winth = (int) (wf * screenHeight);
        int height  = (int) (hf * screenHeight);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(winth);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(height);
        return this;
    }
    public ShowMyTextSearchPopupWindow3 setMyLayputParams2(int winth,int height){
        this.setWidth(winth);
        this.setHeight(height);
        return this;
    }

    /**
     * 设置popupwindow的位置
     * @param parent
     * @param gravity
     * @param x
     * @param y
     * @return
     */
    public ShowMyTextSearchPopupWindow3 setMypopupPosition(View parent, int gravity, int x, int y){
        showAtLocation(parent, gravity, x, y);
        return this;
    }
    public ShowMyTextSearchPopupWindow3 setMypopupPosition2(View parent, int x, int y){
        showAsDropDown(parent, x, y);
        return this;
    }





    /**
     * 设置弹出位置
     */
    public ShowMyTextSearchPopupWindow3 setMyCostomPosition(PopupEm popupEm,View view,int xoff, int yoff) {
        switch (popupEm){
            case bottom://从上部弹出来
                showAsDropDown(view,xoff,yoff);
                break;
        }
        return this;
    }
    private TextView tv_search_area,  tv_search_ks, tv_search_service;
    public ShowMyTextSearchPopupWindow3 initMyTextViewStatus(TextView tv_search_area, TextView tv_search_ks,TextView tv_search_service){
        this.tv_search_area =tv_search_area;
        this.tv_search_ks =tv_search_ks;
        this.tv_search_service =tv_search_service;
        return this;
    }
    public ShowMyTextSearchPopupWindow3 setShowMyTextViewStatus(TextView tv_search_area, TextView tv_search_ks,TextView tv_search_service){
        this.tv_search_area =tv_search_area;
        this.tv_search_ks =tv_search_ks;
        this.tv_search_service =tv_search_service;

        return this;
    }
    @Override
    public void onDismiss() {
        tv_search_area.setTextColor(cnt.getResources().getColor(R.color.gray));
        tv_search_ks.setTextColor(cnt.getResources().getColor(R.color.gray));
        tv_search_service.setTextColor(cnt.getResources().getColor(R.color.gray));
        if(isOpenLoadtion){
            ViewUtil.setTextImgBackground3(tv_search_area, cnt,0,R.drawable.iv_location, 0, R.drawable.inquiey_detail_arrowdown, 0);
        }else{
            ViewUtil.setTextImgBackground3(tv_search_area, cnt, 0,0,  0, R.drawable.inquiey_detail_arrowdown, 0);
        }
        ViewUtil.setTextImgBackground2(tv_search_ks, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.inquiey_detail_arrowdown);
        ViewUtil.setTextImgBackground2(tv_search_service, cnt, ViewUtil.TextMode.RIGHT, 0, R.drawable.inquiey_detail_arrowdown);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(parent == lv_seach_thid_view){
            nowPosition = position;
            listThirdAdapter.setMyNotifyDataChanged(linkedList, nowPosition);
            if(onChecktemLinstener != null){
                onChecktemLinstener.onClickSecondMenu(linkedList.get(nowPosition));
            }
            dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.view_blank:
                if(isShowing()){
                    dismiss();
                }
                break;
        }
    }

    public enum PopupEm{
        top,
        left,
        right,
        bottom
    }
    public void setMyListData( LinkedList<String> subLinkedList){
        linkedList.clear();
        linkedList.addAll(subLinkedList);
        if(listThirdAdapter == null){
            listThirdAdapter = new ListThirdAdapter(cnt,linkedList,nowPosition, new DefaultSelectCallback() {
                @Override
                public void callback(int position) {

                }
            });
            lv_seach_thid_view.setAdapter(listThirdAdapter);
        }else{
            listThirdAdapter.setMyNotifyDataChanged(linkedList,nowPosition);
        }
    }
    public void refreshMylocationData(LinkedList<String> linkedList,boolean isOpenLoadtion){
        this.linkedList = linkedList;
        this.isOpenLoadtion = isOpenLoadtion;

    }
    private OnChecktemLinstener onChecktemLinstener;
    public ShowMyTextSearchPopupWindow3 regisitMyMenuOnClickListaner(OnChecktemLinstener onChecktemLinstener){
        this.onChecktemLinstener = onChecktemLinstener;
        return  this;
    }
}
